#pragma once
#include "../common/dispatcher.hpp"
#include "requestor.hpp"
#include "caller.hpp"
#include "registry.hpp"
#include "topic.hpp"

namespace yjz_rpc
{
    namespace client
    {
        class RegistryClient
        {
        public:
            using ptr = std::shared_ptr<RegistryClient>;
            // 传入注册中心的地址，用于连接注册中心
            RegistryClient(const std::string& ip, int port)
                : _requestor(std::make_shared<Requestor>())
                , _provider(std::make_shared<Provider>(_requestor))
                , _dispatcher(std::make_shared<Dispatcher>())
            {
                auto rsp_cb = std::bind(&client::Requestor::onResponse, _requestor.get(), 
                    std::placeholders::_1, std::placeholders::_2);
                _dispatcher->registerHandler<BaseMessage>(MType::RSP_SERVICE, rsp_cb);

                auto message_cb = std::bind(&Dispatcher::onMessage, _dispatcher.get(), 
                    std::placeholders::_1, std::placeholders::_2);
                _client = ClientFactory::create(ip, port);
                _client->setMessageCallBack(message_cb);
                _client->connect();
            }

            // 向外提供的服务注册接口
            bool serviceRegistry(const std::string& method, const Address& host)
            {
                return _provider->serviceRegistry(_client->getConnect(), method, host);
            }
        private:
            BaseClient::ptr _client;
            Requestor::ptr _requestor;
            Dispatcher::ptr _dispatcher;
            client::Provider::ptr _provider;
        };

        class DiscoveryClient
        {
        public:
            using ptr = std::shared_ptr<DiscoveryClient>;
            // 传入注册中心的地址，用于连接注册中心
            DiscoveryClient(const std::string& ip, int port, const Discoverer::offlineCallback& cb)
                : _requestor(std::make_shared<Requestor>())
                , _dispatcher(std::make_shared<Dispatcher>())
                , _discoverer(std::make_shared<Discoverer>(_requestor, cb))
            {
                auto rsp_cb = std::bind(&client::Requestor::onResponse, _requestor.get(), 
                    std::placeholders::_1, std::placeholders::_2);
                _dispatcher->registerHandler<BaseMessage>(MType::RSP_SERVICE, rsp_cb);

                auto req_cb = std::bind(&client::Discoverer::onServiceRequest, _discoverer.get(),
                    std::placeholders::_1, std::placeholders::_2);
                _dispatcher->registerHandler<ServiceRequest>(MType::REQ_SERVICE, req_cb);

                auto message_cb = std::bind(&Dispatcher::onMessage, _dispatcher.get(), 
                    std::placeholders::_1, std::placeholders::_2);
                _client = ClientFactory::create(ip, port);
                _client->setMessageCallBack(message_cb);
                _client->connect();
            }

            // 向外提供的服务发现接口
            bool serviceDiscovery(const std::string& method, Address& host)
            {
                return _discoverer->serviceDiscovery(_client->getConnect(), method, host);
            }
        private:
            BaseClient::ptr _client;
            Requestor::ptr _requestor;
            Dispatcher::ptr _dispatcher;
            client::Discoverer::ptr _discoverer;
        };

        class RpcClient
        {
        public:
            using ptr = std::shared_ptr<RpcClient>;

            // 是否启用服务发现功能，决定传入的地址是注册中心的地址，还是服务提供者的地址
            RpcClient(bool enableDiscovery, const std::string& ip, int port)
                : _enableDiscovery(enableDiscovery)
                , _requestor(std::make_shared<Requestor>())
                , _dispatcher(std::make_shared<Dispatcher>())
                , _caller(std::make_shared<client::RpcCaller>(_requestor))
            {
                // 针对Rpc请求后的响应进行的回调处理
                auto rsp_cb = std::bind(&client::Requestor::onResponse, _requestor.get(), 
                    std::placeholders::_1, std::placeholders::_2);
                _dispatcher->registerHandler<BaseMessage>(MType::RSP_RPC, rsp_cb);

                if (_enableDiscovery)
                {
                    auto offline_cb = std::bind(&RpcClient::delClient, this, std::placeholders::_1);
                    _discovery_client = std::make_shared<DiscoveryClient>(ip, port, offline_cb);
                }
                else
                {
                    auto message_cb = std::bind(&Dispatcher::onMessage, _dispatcher.get(), 
                        std::placeholders::_1, std::placeholders::_2);
                    _rpc_client = ClientFactory::create(ip, port);
                    _rpc_client->setMessageCallBack(message_cb);
                    _rpc_client->connect();
                }
            }

            // 获取服务提供者：1、服务发现  2、固定服务提供者
            bool call(const std::string& method, const Json::Value& params, Json::Value& result)
            {
                BaseClient::ptr client = getClient(method);
                if (client.get() == nullptr) 
                {
                    return false;
                }
                // 3.通过客户端连接发送Rpc请求
                return _caller->call(client->getConnect(), method, params, result);
            }

            bool call(const std::string& method, const Json::Value& params, RpcCaller::JsonAsyncResponse& result)
            {
                BaseClient::ptr client = getClient(method);
                if (client.get() == nullptr) 
                {
                    return false;
                }
                // 3.通过客户端连接发送Rpc请求
                return _caller->call(client->getConnect(), method, params, result);
            }

            bool call(const std::string& method, const Json::Value& params, const RpcCaller::JsonResponseCallBack& cb)
            {
                BaseClient::ptr client = getClient(method);
                if (client.get() == nullptr) 
                {
                    return false;
                }
                // 3.通过客户端连接发送Rpc请求
                return _caller->call(client->getConnect(), method, params, cb);
            }
        private:
            struct AddressHash
            {
                size_t operator()(const Address& host) const
                {
                    std::string addr = host.first + std::to_string(host.second);
                    return std::hash<std::string>{}(addr);
                }
            };

            BaseClient::ptr newClient(const Address& host)
            {
                auto message_cb = std::bind(&Dispatcher::onMessage, _dispatcher.get(), 
                    std::placeholders::_1, std::placeholders::_2);
                auto client = ClientFactory::create(host.first, host.second);
                client->setMessageCallBack(message_cb);
                client->connect();
                putClient(host, client);
                return client;
            }

            BaseClient::ptr getClient(const Address& host)
            {
                std::unique_lock<std::mutex> lock(_mutex);
                auto it = _rpc_clients.find(host);
                if (it == _rpc_clients.end())
                {
                    return BaseClient::ptr();
                }
                return it->second;
            }

            BaseClient::ptr getClient(const std::string& method)
            {
                BaseClient::ptr client;
                if (_enableDiscovery)
                {
                    // 1.通过服务发现，获取服务提供者地址信息
                    Address host;
                    bool res = _discovery_client->serviceDiscovery(method, host);
                    if (res == false)
                    {
                        LOG(LogLevel::ERROR) << "没有找到 " << method << " 服务的服务提供者！";
                        return BaseClient::ptr();
                    }
                    // 2.查看服务提供者是否已有实例化客户端，有则直接使用，没有则创建
                    client = getClient(host);
                    if (client.get() == nullptr)
                    {
                        client = newClient(host);
                    }
                }
                else
                {
                    client = _rpc_client;
                }
                return client;
            }

            void putClient(const Address& host, const BaseClient::ptr& client)
            {
                std::unique_lock<std::mutex> lock(_mutex);
                _rpc_clients.insert(std::make_pair(host, client));
            }

            void delClient(const Address& host)
            {
                std::unique_lock<std::mutex> lock(_mutex);
                _rpc_clients.erase(host);
            }
        private:
            std::mutex _mutex;
            bool _enableDiscovery;
            Requestor::ptr _requestor;
            RpcCaller::ptr _caller;
            BaseClient::ptr _rpc_client; // 用于未启用服务发现
            Dispatcher::ptr _dispatcher;
            DiscoveryClient::ptr _discovery_client;
            std::unordered_map<Address, BaseClient::ptr, AddressHash> _rpc_clients;
        };

        class TopicClient
        {
        public:
            TopicClient(const std::string& ip, int port)
                : _requestor(std::make_shared<Requestor>())
                , _dispatcher(std::make_shared<Dispatcher>())
                , _topic_manager(std::make_shared<TopicManager>(_requestor))
            {
                auto rsp_cb = std::bind(&Requestor::onResponse, _requestor.get(), 
                    std::placeholders::_1, std::placeholders::_2);
                _dispatcher->registerHandler<BaseMessage>(MType::RSP_TOPIC, rsp_cb);

                auto msg_cb = std::bind(&TopicManager::onPublish, _topic_manager.get(), 
                    std::placeholders::_1, std::placeholders::_2);
                _dispatcher->registerHandler<TopicRequest>(MType::REQ_TOPIC, msg_cb);

                auto message_cb = std::bind(&Dispatcher::onMessage, _dispatcher.get(), 
                    std::placeholders::_1, std::placeholders::_2);
                _rpc_client = ClientFactory::create(ip, port);
                _rpc_client->setMessageCallBack(message_cb);
                _rpc_client->connect();
            }

            bool create(const std::string& key)
            {
                return _topic_manager->create(_rpc_client->getConnect(), key);
            }

            bool remove(const std::string& key)
            {
                return _topic_manager->remove(_rpc_client->getConnect(), key);
            }

            bool subscribe(const std::string& key, const TopicManager::SubCallback& cb)
            {
                return _topic_manager->subscribe(_rpc_client->getConnect(), key, cb);
            }

            bool publish(const std::string& key, const std::string& msg)
            {
                return _topic_manager->publish(_rpc_client->getConnect(), key, msg);
            }

            bool cancel(const std::string& key)
            {
                return _topic_manager->cancel(_rpc_client->getConnect(), key);
            }

            void shutdown()
            {
                _rpc_client->shutdown();
            }
        private:
            Requestor::ptr _requestor;
            Dispatcher::ptr _dispatcher;
            BaseClient::ptr _rpc_client;
            TopicManager::ptr _topic_manager;
        };
    }
}